package project;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/register.do")
public class RegisterServlet1 extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        /*
        //读取请求中的url
        StringBuffer url=req.getRequestURL();
        resp.getWriter().println("url:"+url); //url:http://localhost:8080/web_war/demo03
        resp.getWriter().println("<br>");
        //读取请求行中的请求方法
        String method=req.getMethod();
        resp.getWriter().println("请求方法:"+method); //????:GET
        resp.getWriter().println("<br>");
        //获取servlet映射路径,以/开头
        String servletPath=req.getServletPath();
        resp.getWriter().println("servlet映射路径:"+servletPath); //servlet????:/demo03
        resp.getWriter().println("<br>");
        */

        String username=req.getParameter("username");
        String password=req.getParameter("password");
        resp.getWriter().println("注册成功");
        resp.getWriter().println("<br>");
        resp.getWriter().println("您的用户名username="+username);
        resp.getWriter().println("<br>");
        resp.getWriter().println("您的密码password="+password);
//        if (username.isEmpty() && password.isEmpty()) {
//            resp.getWriter().println("认证错误");
//            resp.getWriter().println("<br>");
//            resp.getWriter().println("用户名和密码为空");
//        }else if(username.isEmpty()&& !password.isEmpty()) {
//            resp.getWriter().println("认证错误");
//            resp.getWriter().println("<br>");
//            resp.getWriter().println("用户名为空");
//        }else if(!username.isEmpty()&&password.isEmpty()) {
//            resp.getWriter().println("认证错误");
//            resp.getWriter().println("<br>");
//            resp.getWriter().println("密码为空");
//        }
//        else {
//            resp.getWriter().println("登录成功");
//            resp.getWriter().println("<br>");
//            resp.getWriter().println("页面输入的username=" + username);
//            resp.getWriter().println("<br>");
//            resp.getWriter().println("页面输入的password=" + password);
//        }

        /*页面发生改变*/
        //重定向 路径发生改变
//        if(req.getParameter("username").length()!=0 && req.getParameter("password").length()!=0){
//            resp.sendRedirect("/success.html");
//        }else if(req.getParameter("username").length()==0 && req.getParameter("password").length()!=0) {
//            resp.sendRedirect("/error1.html");
//        }else if(req.getParameter("username").length()!=0 && req.getParameter("password").length()==0){
//            resp.sendRedirect("/error2.html");
//        }else{
//            resp.sendRedirect("/error.html");
//        }
//        //转发 跳转页面 但是路径没有改变
//        if(req.getParameter("username").length()==0&&(req.getParameter("password").length()==0)){
//            req.getRequestDispatcher("/error.html").forward(req,resp);
//        }else if(req.getParameter("username").length()==0 && req.getParameter("password").length()!=0) {
//            req.getRequestDispatcher("/error1.html").forward(req,resp);
//        }else if(req.getParameter("username").length()!=0 && req.getParameter("password").length()==0){
//            req.getRequestDispatcher("/error2.html").forward(req,resp);
//        }else{
//            req.getRequestDispatcher("/success.html").forward(req,resp);
//        }

    }
}
